-- ModemBroadcast.lua - 发送广播消息测试网络连接性
local component = require("component")
local event = require("event")
local term = require("term")

-- 检查是否有 modem
if not component.isAvailable("modem") then
    print("错误: 未找到 modem 组件")
    return
end

local modem = component.modem
modem.open(12345) -- 打开端口12345

print("Modem 广播测试程序")
print("按 Ctrl+C 退出")
print()

local counter = 1

term.clear()
while true do
    -- 广播消息
    local message = "测试消息 #"..counter
    print("发送: "..message)
    modem.broadcast(12345, message)

    -- 等待5秒或用户中断
    local _, _, char, code = event.pull(5, "interrupted", "key_down")

    if (char and char == 99) or (code and code == 3) then -- c 或 Ctrl+C
        break
    end

    counter = counter + 1
    term.clear()
end

modem.close(12345)
print("程序已终止")
